package com.finchina.myview.ball;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;

/**
 * 作者：黄赛赛 on 2015/9/2 16:19
 *
 * @version V1.0
 * @Description: ${todo}(  使用type animator)
 */
public class BallView extends View {

    final float RADIUS = 50.0f;
    final int DURATION = 5000;

    Point curPoint = null;

    Paint mPaint;
    MyColor curColor;

    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
        mPaint.setColor(Color.parseColor(color));
        invalidate();
    }

    public BallView(Context context) {
        super(context);

    }

    public BallView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (curPoint == null) {
            curPoint = new Point(RADIUS, RADIUS);
            drawCircle(canvas);
            startAnimation();

        } else {
            drawCircle(canvas);
        }
    }

    void drawCircle(Canvas cas) {
        if (mPaint == null) {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(Color.GREEN);
        }
        if (curColor != null)
        mPaint.setColor(Color.argb(curColor.alpha,curColor.red,curColor.green,curColor.blue));
        cas.drawCircle(curPoint.getX(), curPoint.getY(), RADIUS, mPaint);
    }

    void startAnimation() {
        Point start = new Point(RADIUS, RADIUS);
        Point end = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
        ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluate(), start, end);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                curPoint = (Point) animation.getAnimatedValue();
                Log.e("onAnimationUpdate", curPoint.toString());
                invalidate();
            }
        });
//        anim.setDuration(DURATION);
//        anim.start();

//        MyColor startColor = new MyColor(0x7f,0,0xff,0);
//        MyColor endColor = new MyColor(0xff,0xff,0x1e,0xa1);
//        ObjectAnimator anim2 =  ObjectAnimator.ofObject(this, "color", new ColorEvaluate(),startColor, endColor);
//        anim2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                curColor = (MyColor) animation.getAnimatedValue();
//            }
//        });
        ObjectAnimator anim2 =  ObjectAnimator.ofObject(this, "color", new ColorEvaluator2(),"#0000FF", "#FF0000");


        AnimatorSet as = new AnimatorSet();
        as.play(anim).with(anim2);
        as.setDuration(DURATION);
        as.start();
    }
}
